﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Data;

public partial class EarlyWarning_DrugPreWarning_CL : BasePage
{
    public EarlyWarning_DrugPreWarning_CL()
    { }

    int totalrecord = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        //DataTable dt1 = new DataTable();
        //clsTradePreWarning obj = new clsTradePreWarning();
        //dt1 = obj.GetTradePreWarning(this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);//iPreWarning.GetTradePreWarning_CL(mTradePreWarning, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
        //HZ.Supervise.IBLL.IBLL_PreWarning iPreWarning = HZ.Supervise.BLLFactory.IBLL_PreWarning();
        //HZ.Supervise.Model.DrugPreWarning mDrugPreWarning = new DrugPreWarning();
        //mDrugPreWarning.IsUsing = true;
        //DataTable dt2 = new DataTable();
        //dt2 = iPreWarning.GetDrugPreWarning_CL(mDrugPreWarning, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
        //DataTable DTSourse = new DataTable();
        //DTSourse = UniteDataTable(dt1, dt2);        
        HZ.Supervise.Model.WarningData mWarningData = new WarningData();
        HZ.Supervise.IBLL.IBLL_PreWarning iPreWarning = HZ.Supervise.BLLFactory.IBLL_PreWarning();
        mWarningData.AreaId = CurrentAccount.GetUser().Org.Area.AreaID;
        //mWarningData.AreaName = this.txtAreaName.Text.Trim();
        //mWarningData.HospitalName = this.txtHosptailName.Text.Trim();
        //mWarningData.CompanyName_PS = this.txtCompany_PS.Text.Trim();
        DataTable dtWarningData = iPreWarning.GetWarningData(mWarningData, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
        AspNetPager1.RecordCount = totalrecord;
        this.gvwDrugPreWarning.DataSource = dtWarningData;
        this.gvwDrugPreWarning.DataBind();
    }

    /// <summary>
    /// 合并两张表
    /// </summary>
    /// <param name="dt1"></param>
    /// <param name="dt2"></param>
    /// <returns></returns>
    private DataTable UniteDataTable(DataTable dt1, DataTable dt2)
    {
        int a = dt1.Rows.Count;
        int b = dt2.Rows.Count;
        DataTable dt3 = new DataTable();
        dt3.Columns.Add("TradePreWarningID");
        dt3.Columns.Add("DrugPreWarningID");
        dt3.Columns.Add("AreaID");
        dt3.Columns.Add("AreaName");
        dt3.Columns.Add("SortID");
        dt3.Columns.Add("ProcureCatalogID");
        dt3.Columns.Add("HospitalID");
        dt3.Columns.Add("CompanyID_PS");
        dt3.Columns.Add("WarningType");
        dt3.Columns.Add("WarningLevel");
        dt3.Columns.Add("WarningDate");
        dt3.Columns.Add("AddTime");
        dt3.Columns.Add("IsUsing");
        dt3.Columns.Add("HospitalName");
        dt3.Columns.Add("CompanyName_PS");
        dt3.Columns.Add("ProductName");
        dt3.Columns.Add("MedicineModelName");
        dt3.Columns.Add("Outlookc");
        dt3.Columns.Add("WarningCount");
        dt3.Columns.Add("WarningAmount");
        dt3.Columns.Add("CJDW");
        foreach (DataRow dr in dt1.Rows)
        {
            DataRow drM = dt3.NewRow();
            drM["TradePreWarningID"] = dr["TradePreWarningID"].ToString();
            drM["DrugPreWarningID"] = null;
            drM["AreaID"] = dr["AreaID"].ToString();
            drM["AreaName"] = dr["AreaName"].ToString();
            drM["SortID"] = dr["SortID"].ToString();
            drM["ProcureCatalogID"] = dr["ProcureCatalogID"].ToString();
            drM["HospitalID"] = dr["HospitalID"].ToString();
            drM["CompanyID_PS"] = dr["CompanyID_PS"].ToString();
            string s = dr["WarningType"].ToString();
            drM["WarningType"] = dr["WarningType"].ToString();
            drM["WarningLevel"] = dr["WarningLevel"].ToString();
            drM["WarningDate"] = dr["WarningDate"].ToString();
            drM["AddTime"] = dr["AddTime"].ToString();
            drM["IsUsing"] = dr["IsUsing"].ToString();
            drM["HospitalName"] = dr["HospitalName"].ToString();
            drM["CompanyName_PS"] = dr["CompanyName_PS"].ToString();
            drM["ProductName"] = dr["ProductName"].ToString();
            drM["MedicineModelName"] = dr["MedicineModelName"].ToString();
            drM["Outlookc"] = dr["Outlookc"].ToString();
            drM["WarningCount"] = null;
            drM["WarningAmount"] = null;
            drM["CJDW"] = dr["CJDW"].ToString();
            dt3.Rows.Add(drM);
        }
        foreach (DataRow dr in dt2.Rows)
        {
            DataRow drM = dt3.NewRow();
            drM["TradePreWarningID"] = null;
            drM["DrugPreWarningID"] = dr["DrugPreWarningID"].ToString();
            drM["AreaID"] = dr["AreaID"].ToString();
            drM["AreaName"] = dr["AreaName"].ToString();
            drM["SortID"] = dr["SortID"].ToString();
            drM["ProcureCatalogID"] = dr["ProcureCatalogID"].ToString();
            drM["HospitalID"] = dr["HospitalID"].ToString();
            drM["CompanyID_PS"] = null;
            drM["WarningType"] = null;
            drM["WarningLevel"] = dr["WarningLevel"].ToString();
            drM["WarningDate"] = dr["WarningDate"].ToString();
            drM["AddTime"] = dr["AddTime"].ToString();
            drM["IsUsing"] = dr["IsUsing"].ToString();
            drM["HospitalName"] = dr["HospitalName"].ToString();
            drM["CompanyName_PS"] = dr["CompanyName_PS"].ToString();
            drM["ProductName"] = dr["ProductName"].ToString();
            drM["MedicineModelName"] = dr["MedicineModelName"].ToString();
            drM["Outlookc"] = dr["Outlookc"].ToString();
            drM["WarningCount"] = dr["WarningCount"].ToString();
            drM["WarningAmount"] = dr["WarningAmount"].ToString();
            drM["CJDW"] = dr["HospitalName"].ToString();
            dt3.Rows.Add(drM);
        }
        return dt3;
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void gvwDrugPreWarning_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int index = e.Row.RowIndex;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string strWarningLevel = e.Row.Cells[2].Text;
            switch (strWarningLevel)
            {
                case "1":
                    e.Row.Cells[2].Text = "一级预警";
                    break;
                case "2":
                    e.Row.Cells[2].Text = "二级预警";
                    break;
                case "3":
                    e.Row.Cells[2].Text = "三级预警";
                    break;
            }
            string strWarningType = e.Row.Cells[3].Text;
            switch (strWarningType)
            {
                case "0":
                    e.Row.Cells[3].Text = "医院已提交采购单企业未确认";
                    e.Row.Cells[4].Text = gvwDrugPreWarning.DataKeys[index][5].ToString();
                    break;
                case "1":
                    e.Row.Cells[3].Text = "企业已确认定单医院未入库";
                    e.Row.Cells[4].Text = gvwDrugPreWarning.DataKeys[index][4].ToString();
                    break;
                case "2":
                    e.Row.Cells[3].Text = "企业已开发票中心未审核";
                    e.Row.Cells[4].Text = "招标中心";
                    break;
                case "3":
                    e.Row.Cells[3].Text = "医院已提交退货单企业未确认";
                    e.Row.Cells[4].Text = gvwDrugPreWarning.DataKeys[index][5].ToString();
                    break;
                case "4":
                    e.Row.Cells[3].Text = "医院已提交回款单企业未确认";
                    e.Row.Cells[4].Text = gvwDrugPreWarning.DataKeys[index][5].ToString();
                    break;
                case "5":
                    e.Row.Cells[3].Text = "医院已入库未回款";
                    e.Row.Cells[4].Text = gvwDrugPreWarning.DataKeys[index][4].ToString();
                    break;
                default:
                    e.Row.Cells[3].Text = "药品采购量或金额已超";
                    e.Row.Cells[4].Text = gvwDrugPreWarning.DataKeys[index][4].ToString();
                    break;

            }
        }

    }
    protected void gvwDrugPreWarning_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "select")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow selectedRow = gvwDrugPreWarning.Rows[index];
            TableCell contactName = selectedRow.Cells[3];
            string strWarningType = contactName.Text;
            string strAreaID = gvwDrugPreWarning.DataKeys[index][0].ToString();
            string strProcureCatalogID = gvwDrugPreWarning.DataKeys[index][1].ToString();
            string strHospitalID = gvwDrugPreWarning.DataKeys[index][2].ToString();
            string strCompanyID_PS = gvwDrugPreWarning.DataKeys[index][3].ToString();
            string strCompanyName_PS = gvwDrugPreWarning.DataKeys[index][5].ToString();
            string strWarningCount = gvwDrugPreWarning.DataKeys[index][6].ToString();
            string strWarningAmount = gvwDrugPreWarning.DataKeys[index][7].ToString();
            string strWarningDate = gvwDrugPreWarning.Rows[index].Cells[7].Text;

            switch (strWarningType)
            {
                case "医院已提交采购单企业未确认":
                    string url0 = string.Format("~/EarlyWarning/OrderNoConfim.aspx?AreaID={0}&ProcureCatalogID={1}&HospitalID={2}&CompanyID_PS={3}&WarningDate={4}", Server.UrlEncode(strAreaID), Server.UrlEncode(strProcureCatalogID), Server.UrlEncode(strHospitalID), Server.UrlEncode(strCompanyID_PS), Server.UrlEncode(strWarningDate));
                    Response.Redirect(url0);
                    break;
                case "企业已确认定单医院未入库":
                    string url1 = string.Format("~/EarlyWarning/HospitalNoStock.aspx?AreaID={0}&ProcureCatalogID={1}&HospitalID={2}&CompanyID_PS={3}&WarningDate={4}", strAreaID, strProcureCatalogID, strHospitalID, strCompanyID_PS, strWarningDate);
                    Response.Redirect(url1);
                    break;
                case "企业已开发票中心未审核":
                    string url2 = string.Format("~/EarlyWarning/InvoiceNoCheck.aspx?AreaID={0}&ProcureCatalogID={1}&HospitalID={2}&CompanyID_PS={3}&WarningDate={4}&CompanyName_PS={5}", strAreaID, strProcureCatalogID, strHospitalID, strCompanyID_PS, strWarningDate, strCompanyName_PS);
                    Response.Redirect(url2);
                    break;
                case "医院已提交退货单企业未确认":
                    string url3 = string.Format("~/EarlyWarning/ComNoConfimReturnOrder.aspx?AreaID={0}&ProcureCatalogID={1}&HospitalID={2}&CompanyID_PS={3}&WarningDate={4}", strAreaID, strProcureCatalogID, strHospitalID, strCompanyID_PS, strWarningDate);
                    Response.Redirect(url3);
                    break;
                case "医院已提交回款单企业未确认":
                    string url4 = string.Format("~/EarlyWarning/ComNoConfimInvoicePay.aspx?AreaID={0}&ProcureCatalogID={1}&HospitalID={2}&CompanyID_PS={3}&WarningDate={4}", strAreaID, strProcureCatalogID, strHospitalID, strCompanyID_PS, strWarningDate);
                    Response.Redirect(url4);
                    break;
                case "医院已入库未回款":
                    string url5 = string.Format("~/EarlyWarning/InvoiceNoPay.aspx?AreaID={0}&ProcureCatalogID={1}&HospitalID={2}&CompanyID_PS={3}&WarningDate={4}", strAreaID, strProcureCatalogID, strHospitalID, strCompanyID_PS, strWarningDate);
                    Response.Redirect(url5);
                    break;
                default:
                    string url = string.Format("~/EarlyWarning/PurchaseOrderDetail.aspx?AreaID={0}&ProcureCatalogID={1}&HospitalID={2}&WarningCount={3}&WarningAmount={4}&WarningDate={5}", Server.UrlEncode(strAreaID), Server.UrlEncode(strProcureCatalogID), Server.UrlEncode(strHospitalID), Server.UrlEncode(strWarningCount), Server.UrlEncode(strWarningAmount), Server.UrlEncode(strWarningDate));
                    Response.Redirect(url);
                    break;

            }

        }
    }

}
